package com.loper7.base.utils;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.text.DecimalFormat;

/**
 * @author loper7
 * @date 2016/12/14 11:32
 * @Description: ${todo} loper7的日常工具类
 */
public class HUtil {
    public static String ValueOf(EditText editText) {
        if (editText == null)
            return "";
        return editText.getText().toString().trim();
    }

    public static String ValueOf(TextView textView) {
        if (textView == null)
            return "";
        return textView.getText().toString().trim();
    }

    public static String ValueOf(Button button) {
        if (button == null)
            return "";
        return button.getText().toString().trim();
    }

    /**
     * double类型转两位小数字符
     *
     * @param d
     * @return
     */
    public static String double2String(double d) {
        return new DecimalFormat("##0.00").format(d);
    }

    /**
     * double类型转两位小数字符
     *
     * @param d
     * @return
     */
    public static String double2String(String d) {
        if (TextUtils.isEmpty(d))
            return "--";
        return new DecimalFormat("##0.00").format(Double.parseDouble(d));
    }

    /**
     * @param context 上下文
     * @param tv      文本空间
     * @param mipmap  图片资源id
     * @param wind    图片添加位置
     * @param padding 图片距离
     */
    public static final int LEFT = 0, TOP = 1, RIGHT = 2, BOTTOM = 3;

    public static void setTextViewDrawable(Context context, TextView tv, int resId, int wind, int dpPadding) {
        Drawable drawable = null;
        if (resId != 0) {
            drawable = ContextCompat.getDrawable(context, resId);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); //设置边界
        }


        switch (wind) {
            case LEFT:
                tv.setCompoundDrawables(drawable, null, null, null);//画在左边
                break;
            case TOP:
                tv.setCompoundDrawables(null, drawable, null, null);//画在上边
                break;
            case RIGHT:
                tv.setCompoundDrawables(null, null, drawable, null);//画在右边
                break;
            case BOTTOM:
                tv.setCompoundDrawables(null, null, null, drawable);//画在下边
                break;
        }
        tv.setCompoundDrawablePadding(DisplayUtil.dip2px(context, dpPadding));

    }

    /**
     * 在一段文字前添加张图片
     *
     * @param context
     * @param resId
     * @param content
     * @return
     */
    public static SpannableStringBuilder imageAdd2Text(Context context, int resId, String content) {
        //初始化对象
        SpannableStringBuilder sb = new SpannableStringBuilder();
        Drawable d = context.getResources().getDrawable(resId);
        d.setBounds(0, 0, d.getMinimumWidth(), d.getMinimumHeight());//设置图片大小
        sb.append("1");
        sb.setSpan(new ImageSpan(d), 0, 1, Spannable.SPAN_COMPOSING);
        sb.append(content);

        return sb;
    }

    /**
     * 直接拨打电话
     *
     * @param context 上下文
     * @param tel     电话号码
     */
    public static void call(Context context, String tel) {
        Uri uri = Uri.parse("tel:" + tel);
        Intent intent = new Intent(Intent.ACTION_DIAL, uri);
        context.startActivity(intent);
    }

}
